import { Component, OnInit, OnDestroy, Input, ViewChild, ElementRef, AfterViewChecked  } from '@angular/core';
import {Type, Message} from '../entity/Chat'

import {SendboxComponent} from '../chat/sendbox/sendbox.component'
import {MessageboxComponent} from '../chat/messagebox/messagebox.component'

import {ChatService} from '../services/chat.service'
import { NGXLogger } from 'ngx-logger';

@Component({
  selector: 'app-chat',
  templateUrl: './chat.component.html',
  styleUrls: ['./chat.component.scss']
})
export class ChatComponent implements OnInit, OnDestroy, AfterViewChecked {

  @ViewChild(SendboxComponent, {static: false})
  private sendboxComponent: SendboxComponent;

  @ViewChild(MessageboxComponent, {static: false})
  private messageboxComponent : MessageboxComponent;

  @ViewChild(MessageboxComponent, {static: false})
  private messageboxRef: ElementRef;

  constructor(
    private log: NGXLogger,
    private chatService: ChatService) { }

  ngOnInit() {

  }

  ngOnDestroy(){

  }

  send(){
    let sendbox = this.sendboxComponent;
    let text: string = sendbox.getText();
    this.log.debug('send:', text)

    let b = this.chatService.sendMessage(text);

    if(b)
      setTimeout(this.onSend.bind(this), 400)

  }

  onSend(){

    let sendbox = this.sendboxComponent;
    sendbox.clear();

    // let ne = this.messageboxRef.nativeElement;
    // this.log.debug("scrollHeight", ne)
    // let h = ne.scrollHeight;
    // this.log.debug("scrollHeight", h)

  }

  ngAfterViewChecked() {

  }

}
